<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Cost extends Page
{
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $hfincident = null;
       public $hfveh = null;
       public $JTCost = null;
       public $JTSiteTheme1 = null;

        function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }



       function CostCreate($sender, $params)
       {
       $incident = $_GET['incident'];
       $this->hfincident->setValue($incident);


       }

       function JTCostShow($sender, $params)
       {
       $query_cost = "select * from incident_cost_tbl where incident_id='".$this->hfincident->getValue()."'";
       $result_cost = mysql_query($query_cost)or die(mysql_error());


       $arr = array();
        while($row = mysql_fetch_array($result_cost))
          {
          $incident_id = $this->hfincident->getValue();
          $cost = $row['auto_id'];
          $tools = "<img onclick=js_deleteCost('".$incident_id."','".$cost."') src='image/small_del.png' title='delete' style='cursor:pointer'/>";
          $desc = "<span style='color:blue;text-decoration:underline;cursor:pointer' onClick=editCost('".$cost."') title='Edit Cost'>".$row['description']."</span>";

          if($_SESSION['access_vehicle']==1)
          {
          $arr[] = array(
                        'Description' => $desc,
                        'Cost' => "<div style align='right'>".$row['amount']."</div>"
                        );
          }

          if($_SESSION['access_vehicle']==2)
          {
          $arr[] = array(
                        'Description' => $desc,
                        'Cost' => "<div style align='right'>".$row['amount']."</div>",
                        'Tools' => $tools
                        );
          }

          }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM incident_cost_tbl where incident_id='".$this->hfincident->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);

      $tot_amt = $row_amount['sum_amount'];
      $ctot_amt = $this->commafy($tot_amt);

      $arr[] = array(
                        'Description' => TOTAL,
                        'Cost' => "<div style align='right'>".$ctot_amt."</div>"
                        );


        $this->JTCost->setCellData($arr);


       }


}

global $application;

global $Cost;

//Creates the form
$Cost=new Cost($application);

//Read from resource file
$Cost->loadResource(__FILE__);

//Shows the form
$Cost->show();

?>